﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccess.Repository.Interface;
using System.Data.SqlClient;
using System.Data;


namespace DataAccess.Repository
{
    public class IMPPolicyDocument : IPolicyDocuments
    {
        private KPIDbContext _KPIDBContext = new KPIDbContext();

        #region 新增行政文件
        /// <summary>
        /// 新增行政文件
        /// </summary>
        /// <param name="p">行政文件对象</param>
        /// <returns></returns>
        public bool CreatePolicyDocument(Models.PolicyDocuments p)
        {
            var result = false;
            try
            {
                _KPIDBContext.PolicyDocuments.Add(p);
                _KPIDBContext.SaveChanges();
                result = true;
            }
            catch (Exception)
            {
                
                throw;
            }
            return result;
        }
        #endregion

        #region 获取行政文件分页
        /// <summary>
        /// 获取行政文件分页
        /// </summary>
        /// <param name="keys">名称</param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageCount"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IList<Models.PolicyDocuments> GetPolicyDocumentsListPageing(string keys, int PageSize, int PageIndex, out int PageCount, out int rowCount)
        {
            IList<Models.PolicyDocuments> List = new List<Models.PolicyDocuments>();

            SqlParameter p1 = new SqlParameter("@PageCount", null);
            p1.DbType = DbType.Int32;
            p1.Direction = ParameterDirection.Output;

            SqlParameter p2 = new SqlParameter("@rowCount", null);
            p2.DbType = DbType.Int32;
            p2.Direction = ParameterDirection.Output;

            SqlParameter[] sp = new SqlParameter[]{
               new SqlParameter("@pagesize",PageSize),
               new SqlParameter("@pageindex",PageIndex),
               p1,p2,
                new SqlParameter("@keys",keys)
           };
           
            DataTable dtRes = SqlHelper.GetDataSet(CommandType.StoredProcedure, "proc_selectPolicyDocumentsPageing", sp);
            PageCount = Convert.ToInt32(p1.Value);
            rowCount = Convert.ToInt32(p2.Value);

            foreach (DataRow reader in dtRes.Rows)
            {
                Models.PolicyDocuments pd = new Models.PolicyDocuments();
                pd.PolicyDocumentID = Convert.ToInt32(reader["PolicyDocumentID"]);
                pd.PolicyDocumentTitle = reader["PolicyDocumentTitle"].ToString();
                pd.PolicyDocumentContent = reader["PolicyDocumentContent"].ToString();
                pd.PolicyDocumentAddTime = Convert.ToDateTime(reader["PolicyDocumentAddTime"].ToString());
                pd.PolicyDocumentModifyTime = Convert.ToDateTime(reader["PolicyDocumentModifyTime"].ToString());
                List.Add(pd);
            }
            return List;
        }

        #endregion

        #region 获取行政文件信息内容
        /// <summary>
        /// 获取行政文件信息内容
        /// </summary>
        /// <param name="pdid"></param>
        /// <returns></returns>
        public Models.PolicyDocuments GetDetailPolicyDocuments(int pdid)
        {
           return _KPIDBContext.PolicyDocuments.FirstOrDefault(pd => pd.PolicyDocumentID == pdid);
        }

        #endregion

        #region 编辑行政文件
        /// <summary>
        /// 编辑行政文件
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool EditPolicyDocument(Models.PolicyDocuments p)
        {
            var result = false;

            if (p != null && p.PolicyDocumentID > 0)
            {
                try
                {
                    _KPIDBContext.Entry(p).State = EntityState.Modified;
                    _KPIDBContext.SaveChanges();

                     result = true;
                }
                catch
                {
                    throw;
                }
            }
            return result;
        }
        #endregion

        #region 删除行政文件
        /// <summary>
        /// 删除行政文件
        /// </summary>
        /// <param name="pdIDs"></param>
        /// <returns></returns>
        public bool DeletePolicyDocuments(List<int> pdIDs)
        {
            var result = false;

            if (pdIDs != null && pdIDs.Count > 0)
            {
                try
                {
                    foreach (var id in pdIDs)
                    {
                        var pd = _KPIDBContext.PolicyDocuments.Find(id);

                        if (pd != null)
                        {
                            _KPIDBContext.PolicyDocuments.Remove(pd);
                        }
                    }

                    if (_KPIDBContext.SaveChanges() > 0)
                    {
                        result = true;
                    }
                }
                catch
                {
                    throw;
                }
            }
            return result;
        }
        #endregion
    }
}
